home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1998 August / PC Plus SuperCD 50b Issue 142 (CD142b) (August 1998).iso / handson / Delphi / delph142.exe / ThRunStop / thrs.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-04-06  |  1.4 KB  |  71 lines

  1. unit thrs;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  7.   Forms, Dialogs, StdCtrls;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     Edit1: TEdit;
  12.     RunBtn: TButton;
  13.     StopBtn: TButton;
  14.     procedure RunBtnClick(Sender: TObject);
  15.     procedure StopBtnClick(Sender: TObject);
  16.     procedure FormActivate(Sender: TObject);
  17.   private
  18.     { Private declarations }
  19.   public
  20.     { Public declarations }
  21.   end;
  22.  
  23. var
  24.   Form1: TForm1;
  25.   Stop : boolean;
  26.  
  27.   thHandle : integer;
  28.   thID : integer;
  29.  
  30. implementation
  31.  
  32. {$R *.DFM}
  33.  
  34. function Go ( p : Pointer ) : integer;
  35. { The Thread function. This runs until the Boolean Stop = true
  36.   then it exits. The Thread is terminated on exit }
  37. var
  38.  i : Longint;
  39. begin
  40.  Stop := false;
  41.  for i := 0 to 2000 do
  42.  begin
  43.   Form1.Edit1.Text := IntToStr(i);
  44. //  Form1.Edit1.Update;    { this is no longer needed }
  45.   if Stop then exit;
  46.  end;
  47. end;
  48.  
  49. procedure TForm1.RunBtnClick(Sender: TObject);
  50. { Begin a Thread to run the Function named Go }
  51. begin
  52.   try
  53.     thHandle := BeginThread(nil,0,Go,nil,0,thID);
  54.   finally
  55.     if thHandle <> 0 then       // if 0 there was an error
  56.        CloseHandle(thHandle);   // close handle after Thread is dead!
  57.   end;
  58. end;
  59.  
  60. procedure TForm1.StopBtnClick(Sender: TObject);
  61. begin
  62.   Stop := true;
  63. end;
  64.  
  65. procedure TForm1.FormActivate(Sender: TObject);
  66. begin
  67.   Stop := false;
  68. end;
  69.  
  70. end.
  71.